package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.CourseMode;
import com.think.teaching.service.impl.CourseModeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程形式 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "课程形式接口")
public class CourseModeController {
	
	private final CourseModeServiceImpl courseModeService;

	public CourseModeController(CourseModeServiceImpl courseModeService) {
		this.courseModeService = courseModeService;
	}

	@ApiOperation("添加新的课程形式")
	@PostMapping("admin/courseMode")
	public Result AddCourseMode(@RequestBody()CourseMode data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			CourseMode courseMode = courseModeService.AddCourseMode(data);
			if( courseMode!= null) {
				return Result.success(courseMode);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新课程形式")
	@RequestMapping(value = "admin/courseMode",method = RequestMethod.PUT)
	public Result UpdateCourseMode(@RequestBody()CourseMode data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			CourseMode courseMode = courseModeService.UpdateCourseMode(data);
			if( courseMode!= null) {
				return Result.success(courseMode);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除课程形式")
	@RequestMapping(value = "admin/courseMode/{id}",method = RequestMethod.DELETE)
	public Result DeleteCourseMode(@PathVariable("id")Integer id) {
		CourseMode courseMode = courseModeService.getById(id);
		if( courseMode== null) {
			return Result.failure(ResultCode.COURSE_MODE_NOT_EXIST);
		}
		else {
			if (courseModeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("获取课程形式详情")
	@RequestMapping(value = "admin/courseMode/{id}",method = RequestMethod.GET)
	public Result GetCourseMode(@PathVariable("id")Integer id) {
		CourseMode courseMode = courseModeService.getById(id);
		if( courseMode== null) {
			return Result.failure(ResultCode.COURSE_MODE_NOT_EXIST);
		}
		else {
			
			return Result.success(courseMode);
		}
	}
	
	@ApiOperation("管理员修改课程形式状态")
	@RequestMapping(value = "admin/courseMode/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateCourseModeState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		CourseMode courseMode = courseModeService.getById(id);
		if( courseMode== null) {
			return Result.failure(ResultCode.COURSE_MODE_NOT_EXIST);
		}
		else {
			try {
				courseMode = courseModeService.SetCourseModeState(id, state);
				return Result.success(courseMode);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索课程形式")
	@RequestMapping(value = "user/courseMode/search",method = RequestMethod.GET)
	public Result UserSearchCourseMode(@RequestParam("key")String key) {
		return Result.success(courseModeService.SearchCourseModeWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索课程形式")
	@RequestMapping(value = "admin/courseMode/search",method = RequestMethod.GET)
	public Result AdminSearchCourseMode(@RequestParam("key")String key) {
		return Result.success(courseModeService.SearchCourseModeWithKey(key, false));
	}
	
	@ApiOperation("管理员获取课程形式列表")
	@RequestMapping(value = "admin/courseModes/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListCourseMode(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("tag")Integer tag,
			Integer type) {
		return Result.success(courseModeService.ListCourseModeWithTagAndType(count, page, type, tag, false));
	}
	
	@ApiOperation("用户获取课程形式列表")
	@RequestMapping(value = "user/courseModes/{page}/{count}",method = RequestMethod.GET)
	public Result UserListCourseMode(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("tag")Integer tag,
			Integer type) {
		return Result.success(courseModeService.ListCourseModeWithTagAndType(count, page, type, tag, true));
	}

}

